package com.forsir.android.mhfinance.storeObjects;

import android.content.ContentValues;
import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public class Subcategory extends IStorable {
	private String mName;
	private long mParentId;

	// only for report
	public float summaryWithdraw = 0;
	public float summaryDeposit = 0;
	public int count = 0;

	public Subcategory() {
	}

	public Subcategory(int id) {
		setId(id);
	}

	public Subcategory(Cursor cursor) {
		fillFromCursor(cursor);
	}

	public Subcategory(String name) {
		mName = name;
	}

	public void setName(String name) {
		mName = name;
	}

	@Override
	public String getName() {
		return mName;
	}

	public String getFullName() {
		final Category category = CategoryList.getCategoryById(mParentId);
		if (category == null) {
			return mName;
		}
		return category.getName() + " - " + mName;
	}

	public void setParentId(long parentId) {
		mParentId = parentId;
	}

	public long getParentId() {
		return mParentId;
	}

	@Override
	public ContentValues getContent() {
		final ContentValues content = new ContentValues();
		content.put(DbAdapter.CATEGORY_NAME, getName());
		content.put(DbAdapter.CATEGORY_PARENT, getParentId());
		return content;
	}

	@Override
	public String getTableName() {
		return DbAdapter.CATEGORY_TABLE;
	}

	@Override
	public String[] getColumns() {
		return new String[] { DbAdapter.ID, DbAdapter.CATEGORY_PARENT, DbAdapter.CATEGORY_NAME };
	}

	@Override
	/*
	 * * gets values from actual position of cursor
	 */
	public IStorable fillFromCursor(Cursor cursor) {
		if (cursor == null) {
			setId(-1);
			mName = "";
			return this;
		}

		if (!cursor.isAfterLast()) {
			setId(cursor.getLong(cursor.getColumnIndex(DbAdapter.ID)));
			mParentId = cursor.getLong(cursor.getColumnIndex(DbAdapter.CATEGORY_PARENT));
			mName = cursor.getString(cursor.getColumnIndex(DbAdapter.CATEGORY_NAME));
		}

		return this;
	}

}
